#include <QPalette>
#include <QPainter>

#include "titleBar.h"
#include "globalValue.h"


titleBar::titleBar(QWidget *parent) : QWidget(parent)
{


    this->setFixedHeight(40);
    this->setMinimumWidth(100);

    wherePointing = 0;
    this->setMouseTracking(true);

    isMousePressed = 0;

}


void titleBar::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen pen;
    QBrush brush;
    QColor color;
    QFont font;

    painter.setRenderHint(QPainter::Antialiasing, true);
    brush.setStyle(Qt::SolidPattern);

    color.setRgb(0x22, 0x22, 0x22, 0x22);
    painter.fillRect(this->rect(), widgetBack);


    if(wherePointing == 1)
    {
        pen.setWidth(0);
        color.setRgb(0x00, 0x22, 0xAA, 0x88);
        pen.setColor(color);
        brush.setColor(color);
        painter.setPen(pen);
        painter.setBrush(brush);
        painter.drawRect(QRectF(this->width()-80-5, 0, 40, 40));

    }
    else if(wherePointing == 2)
    {
        pen.setWidth(0);
        color.setRgb(0xAA, 0x00, 0x00, 0xCC);
        pen.setColor(color);
        brush.setColor(color);
        painter.setPen(pen);
        painter.setBrush(brush);
        painter.drawRect(QRectF(this->width()-40-5, 0, 40, 40));


    }


    pen.setWidth(2);
    pen.setCapStyle(Qt::RoundCap);
    color.setRgb(0xAA, 0xAA, 0xAA, 0xCC);
    pen.setColor(color);
    painter.setPen(pen);

    painter.drawLine(QPointF(this->width()-30-5, 10),
                     QPointF(this->width()-10-5, 30));
    painter.drawLine(QPointF(this->width()-30-5, 30),
                     QPointF(this->width()-10-5, 10));
    painter.drawLine(QPointF(this->width()-70-5, 30),
                     QPointF(this->width()-50-5, 30));

    color.setRgb(0xFF, 0xFF, 0xFF);
    pen.setColor(color);
    painter.setPen(pen);
    font = painter.font();
    font.setPixelSize(25);
    painter.setFont(font);

    painter.drawText(150, 30, QObject::tr("音频播放器"));


}

void titleBar::mouseReleaseEvent(QMouseEvent *event)
{
    isMousePressed = 0;
}

void titleBar::mousePressEvent(QMouseEvent *event)
{
    if(event->x() >= this->width()-80-5
            && event->x() <= this->width()-40-5)
    {
        emit miniWindows();
    }
    else if(event->x() >= this->width()-40-5
            && event->x() <= this->width()-5)
    {
        emit closeWindows();
    }
    else
    {
        isMousePressed = 1;
        lastPoint = event->globalPos();
        emit titlePressed(event);
    }
}

void titleBar::mouseMoveEvent(QMouseEvent *event)
{
    if(event->x() >= this->width()-80-5
            && event->x() <= this->width()-40-5)
    {
        wherePointing = 1;
        this->setCursor(Qt::PointingHandCursor);
    }
    else if(event->x() >= this->width()-40-5
            && event->x() <= this->width()-5)
    {
        wherePointing = 2;
        this->setCursor(Qt::PointingHandCursor);
    }
    else
    {
        wherePointing = 0;
        this->setCursor(Qt::ArrowCursor);
        if(isMousePressed == 1)
        {

            emit moveWindows(event);
            lastPoint = event->globalPos();
        }
    }

    update();
}

void titleBar::enterEvent(QEvent *event)
{}

void titleBar::leaveEvent(QEvent *event)
{
    wherePointing = 0;
    this->setCursor(Qt::ArrowCursor);
    update();

}
